{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 判断文件是否存在"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "os.path.isfile(\"2064.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "os.path.isfile(\"notExist.csv\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 创建与删除文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "if os.path.isfile(\"newfile.txt\"):\n",
    "    os.remove(\"newfile.txt\")\n",
    "print (os.path.isfile(\"newfile.txt\"))\n",
    "fr = open('newfile.txt', 'w+')\n",
    "fr.close()\n",
    "print (os.path.isfile(\"newfile.txt\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "if not os.path.isfile(\"newfile.txt\"):\n",
    "    fr = open('newfile.txt', 'w+')\n",
    "    fr.close()\n",
    "print (os.path.isfile(\"newfile.txt\"))\n",
    "os.remove(\"newfile.txt\")\n",
    "print (os.path.isfile(\"newfile.txt\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 覆盖写与追加写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "second sentence\n"
     ]
    }
   ],
   "source": [
    "if os.path.isfile(\"newfile.txt\"):\n",
    "    os.remove(\"newfile.txt\")\n",
    "fr = open('newfile.txt', 'w+')\n",
    "fr.write(\"first sentence\\r\\n\")\n",
    "fr.close()\n",
    "fr = open('newfile.txt', 'w+')\n",
    "fr.write(\"second sentence\")\n",
    "fr.close()\n",
    "fr = open('newfile.txt')\n",
    "print (fr.read())\n",
    "fr.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "first sentence\n",
      "\n",
      "second sentence\n"
     ]
    }
   ],
   "source": [
    "if os.path.isfile(\"newfile.txt\"):\n",
    "    os.remove(\"newfile.txt\")\n",
    "fr = open('newfile.txt', 'a+')\n",
    "fr.write(\"first sentence\\r\\n\")\n",
    "fr.close()\n",
    "fr = open('newfile.txt', 'a+')\n",
    "fr.write(\"second sentence\")\n",
    "fr.close()\n",
    "fr = open('newfile.txt')\n",
    "print (fr.read())\n",
    "fr.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 判断目录是否存在"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "os.path.exists('test_dir')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 列出路径下有哪些文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['.gitignore', '.ipynb_checkpoints']"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "os.listdir('test_dir')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 创建路径\n",
    "\n",
    "```python\n",
    "os.makedirs(dir_name)\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
